home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Text / Show / Less / less-252 / main.c < prev    next >
C/C++ Source or Header  |  1994-10-15  |  7KB  |  329 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Entry point, initialization, miscellaneous routines.
  30.  */
  31.  
  32. #include "less.h"
  33. #include "position.h"
  34.  
  35. public int    ispipe;
  36. public char *    every_first_cmd = NULL;
  37. public int    new_file;
  38. public int    is_tty;
  39. public IFILE    curr_ifile = NULL_IFILE;
  40. public IFILE    old_ifile = NULL_IFILE;
  41. public struct scrpos initial_scrpos;
  42. public int    any_display = 0;
  43. public int    wscroll;
  44. public char *    progname;
  45. public int    quitting;
  46.  
  47. extern int    file;
  48. extern int    fd0;
  49. extern int    quit_at_eof;
  50. extern int    cbufs;
  51. extern int    errmsgs;
  52. extern int    screen_trashed;
  53. extern int    force_open;
  54.  
  55. #if LOGFILE
  56. public int    logfile = -1;
  57. public int    force_logfile = 0;
  58. public char *    namelogfile = NULL;
  59. #endif
  60.  
  61. #if EDITOR
  62. public char *    editor;
  63. public char *    editproto;
  64. #endif
  65.  
  66. #if TAGS
  67. extern char *    tagfile;
  68. extern char *    tagoption;
  69. extern int    jump_sline;
  70. #endif
  71.  
  72.  
  73.  
  74. /*
  75.  * Entry point.
  76.  */
  77. int
  78. main(argc, argv)
  79.     int argc;
  80.     char *argv[];
  81. {
  82.     IFILE ifile;
  83.  
  84.     progname = *argv++;
  85.  
  86.     /*
  87.      * Process command line arguments and LESS environment arguments.
  88.      * Command line arguments override environment arguments.
  89.      */
  90.     init_prompt();
  91.     init_charset();
  92.     init_option();
  93.     scan_option(getenv("LESS"));
  94.  
  95. #define    isoptstring(s)    (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
  96.     while (--argc > 0 && (isoptstring(argv[0]) || isoptpending()))
  97.         scan_option(*argv++);
  98. #undef isoptstring
  99.  
  100.     if (isoptpending())
  101.     {
  102.         /*
  103.          * Last command line option was a flag requiring a
  104.          * following string, but there was no following string.
  105.          */
  106.         nopendopt();
  107.         quit(0);
  108.     }
  109.  
  110. #if EDITOR
  111.     editor = getenv("EDITOR");
  112.     if (editor == NULL || *editor == '\0')
  113.         editor = EDIT_PGM;
  114.     editproto = getenv("LESSEDIT");
  115.     if (editproto == NULL || *editproto == '\0')
  116.         editproto = "%E ?lm+%lm. %f";
  117. #endif
  118.  
  119.     /*
  120.      * Call get_ifile with all the command line filenames
  121.      * to "register" them with the ifile system.
  122.      */
  123.     ifile = NULL_IFILE;
  124.     while (--argc >= 0)
  125.     {
  126. #if MSOFTC
  127.         /*
  128.          * Because the "shell" doesn't expand filename patterns,
  129.          * treat each argument as a filename pattern rather than
  130.          * a single filename.  
  131.          * Expand the pattern and iterate over the expanded list.
  132.          */
  133.         {
  134.             struct textlist tlist;
  135.             char *gfilename;
  136.             char *filename;
  137.             
  138.             gfilename = glob(*argv++);
  139.             init_textlist(&tlist, gfilename);
  140.             filename = NULL;
  141.             while ((filename = forw_textlist(&tlist, filename)) != NULL)
  142.                 ifile = get_ifile(filename, ifile);
  143.             free(gfilename);
  144.         }
  145. #else
  146.         ifile = get_ifile(*argv++, ifile);
  147. #endif
  148.     }
  149.     /*
  150.      * Set up terminal, etc.
  151.      */
  152.     is_tty = isatty(1);
  153.     if (!is_tty)
  154.     {
  155.         /*
  156.          * Output is not a tty.
  157.          * Just copy the input file(s) to output.
  158.          */
  159.         if (nifile() == 0)
  160.         {
  161.             if (edit_stdin() == 0)
  162.                 cat_file();
  163.         } else if (edit_first() == 0)
  164.         {
  165.             do {
  166.                 cat_file();
  167.             } while (edit_next(1) == 0);
  168.         }
  169.         quit(0);
  170.     }
  171.  
  172.     init_mark();
  173.     init_cmds();
  174.     raw_mode(1);
  175.     get_term();
  176.     get_editkeys();
  177. #if USERFILE
  178.     /*
  179.      * Try to use the lesskey file "$HOME/.less".
  180.      */
  181.     add_hometable();
  182. #endif
  183.     open_getchr();
  184.     init_signals(1);
  185.  
  186.     /*
  187.      * Select the first file to examine.
  188.      */
  189. #if TAGS
  190.     if (tagoption != NULL)
  191.     {
  192.         /*
  193.          * A -t option was given.
  194.          * Verify that no filenames were also given.
  195.          * Edit the file selected by the "tags" search,
  196.          * and search for the proper line in the file.
  197.          */
  198.         if (nifile() > 0)
  199.         {
  200.             error("No filenames allowed with -t option", NULL_PARG);
  201.             quit(1);
  202.         }
  203.         findtag(tagoption);
  204.         if (tagfile == NULL)
  205.             quit(1);
  206.         if (edit(tagfile))  /* Edit file which contains the tag */
  207.             quit(1);
  208.         /*
  209.          * Search for the line which contains the tag.
  210.          * Set up initial_scrpos so we display that line.
  211.          */
  212.         initial_scrpos.pos = tagsearch();
  213.         if (initial_scrpos.pos == NULL_POSITION)
  214.             quit(1);
  215.         initial_scrpos.ln = jump_sline;
  216.     } else
  217. #endif
  218.     if (nifile() == 0)
  219.     {
  220.         if (edit_stdin())  /* Edit standard input */
  221.             quit(1);
  222.     } else 
  223.     {
  224.         if (edit_first())  /* Edit first valid file in cmd line */
  225.             quit(1);
  226.     }
  227.  
  228.     init();
  229.     commands();
  230.     quit(0);
  231.     /*NOTREACHED*/
  232. }
  233.  
  234. /*
  235.  * Copy a string, truncating to the specified length if necessary.
  236.  * Unlike strncpy(), the resulting string is guaranteed to be null-terminated.
  237.  */
  238.     public void
  239. strtcpy(to, from, len)
  240.     char *to;
  241.     char *from;
  242.     unsigned int len;
  243. {
  244.     strncpy(to, from, len);
  245.     to[len-1] = '\0';
  246. }
  247.  
  248. /*
  249.  * Copy a string to a "safe" place
  250.  * (that is, to a buffer allocated by calloc).
  251.  */
  252.     public char *
  253. save(s)
  254.     char *s;
  255. {
  256.     register char *p;
  257.  
  258.     p = (char *) ecalloc(strlen(s)+1, sizeof(char));
  259.     strcpy(p, s);
  260.     return (p);
  261. }
  262.  
  263. /*
  264.  * Allocate memory.
  265.  * Like calloc(), but never returns an error (NULL).
  266.  */
  267.     public VOID_POINTER
  268. ecalloc(count, size)
  269.     int count;
  270.     unsigned int size;
  271. {
  272.     register VOID_POINTER p;
  273.  
  274.     p = (VOID_POINTER) calloc(count, size);
  275.     if (p != NULL)
  276.         return (p);
  277.     error("Cannot allocate memory", NULL_PARG);
  278.     quit(1);
  279.     /*NOTREACHED*/
  280. }
  281.  
  282. /*
  283.  * Skip leading spaces in a string.
  284.  */
  285.     public char *
  286. skipsp(s)
  287.     register char *s;
  288. {
  289.     while (*s == ' ' || *s == '\t')    
  290.         s++;
  291.     return (s);
  292. }
  293.  
  294. /*
  295.  * Exit the program.
  296.  */
  297.     public void
  298. quit(status)
  299.     int status;
  300. {
  301.     static int save_status;
  302.  
  303.     /*
  304.      * Put cursor at bottom left corner, clear the line,
  305.      * reset the terminal modes, and exit.
  306.      */
  307.     if (status < 0)
  308.         status = save_status;
  309.     else
  310.         save_status = status;
  311.     quitting = 1;
  312.     edit((char*)NULL);
  313.     if (any_display)
  314.         clear_bot();
  315.     deinit();
  316.     flush();
  317.     raw_mode(0);
  318. #if MSOFTC
  319.     /* 
  320.      * If we don't close 2, we get some garbage from
  321.      * 2's buffer when it flushes automatically.
  322.      * I cannot track this one down  RB
  323.      * The same bug shows up if we use ^C^C to abort.
  324.      */
  325.     close(2);
  326. #endif
  327.     exit(status);
  328. }
  329.